<script>

import VipDetail from '@/views/admin/vip/VipDetail.vue'
import VipExam from '@/views/admin/vip/VipExam.vue'

export default {
  name: 'VipList',
  components: { VipExam, VipDetail },
  data () {
    return {
      pageInfo: {
        page: 1,
        limit: 5,
        total: 0
      },
      searchInfo: {
        username: '',
        nickname: '',
        phone: '',
        status: '',
        userinfoStatus: ''
      },
      tableList: [],
      delIds: []
    }
  },
  methods: {
    getTableList () {
      this.$axios.get('vip/page', {
        params: {
          ...this.pageInfo,
          ...this.searchInfo
        }
      }).then(data => {
        this.tableList = [...data.list]
        this.pageInfo.total = data.total
      }).catch(e => e)
    },
    currentChange (val) {
      this.pageInfo.page = val
      this.getTableList()
    },
    sizeChange (val) {
      this.pageInfo.page = 1
      this.pageInfo.limit = val
      this.getTableList()
    },
    search () {
      this.pageInfo.page = 1
      this.getTableList()
    },
    clear () {
      this.pageInfo = { ...this.$options.data().pageInfo }
      this.searchInfo = { ...this.$options.data().searchInfo }
      this.getTableList()
    },
    changeStatus (userinfoId) {
      this.$confirm(`确定修改编号[${userinfoId}]的状态吗?`, '询问', { type: 'info' }).then(() => {
        this.$axios.post(`vip/update/status/${userinfoId}`).then(() => {
          this.$message.success('操作成功！')
          this.getTableList()
        }).catch(e => e)
      }).catch(() => {})
    },
    detail (id) {
      this.$refs.vipDetail.init(id)
    },
    exam (type, id) {
      this.$refs.vipExam.init(type, id)
    },
    selectionChange (selection) {
      this.delIds = selection.map(item => item.userinfoId)
    },
    del (id) {
      let ids = []
      if (id) {
        ids = [id]
      } else {
        if (this.delIds.length === 0) {
          this.$message.error('请至少选中一条数据进行删除！')
          return false
        } else {
          ids = [...this.delIds]
        }
      }
      this.$confirm(`确定删除数据[${ids}]的数据吗?`, '询问').then(() => {
        this.$axios.delete('vip/delete', {
          params: { ids: ids.toString() }
        }).then(() => {
          this.$message.success('删除成功')
          this.getTableList()
        }).catch(e => e)
      }).catch(() => {})
    }
  },
  created () {
    this.getTableList()
  }
}
</script>

<template>
<div>
  <el-card>
    <el-form :model="searchInfo" label-width="80px" inline>
      <el-form-item label="用户名">
        <el-input v-model="searchInfo.username" clearable placeholder="用户名"/>
      </el-form-item>
      <el-form-item label="昵称">
        <el-input v-model="searchInfo.nickname" clearable placeholder="昵称"/>
      </el-form-item>
      <el-form-item label="手机号">
        <el-input v-model="searchInfo.phone" clearable placeholder="手机号码"/>
      </el-form-item>
      <el-form-item label="实名状态">
        <el-select v-model="searchInfo.userinfoStatus" clearable placeholder="实名状态">
          <el-option :value="1" label="已注册"/>
          <el-option :value="2" label="已提交"/>
          <el-option :value="3" label="已实名"/>
          <el-option :value="4" label="已拒绝"/>
        </el-select>
      </el-form-item>
      <el-form-item label="账号状态">
        <el-select v-model="searchInfo.status" clearable placeholder="账号状态">
          <el-option :value="1" label="可用"/>
          <el-option :value="2" label="禁用"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="search">查询</el-button>
        <el-button @click="clear">清空</el-button>
      </el-form-item>
    </el-form>
  </el-card>
  <el-card>
    <el-button @click="del()">删除</el-button>
    <el-table :data="tableList" border @selection-change="selectionChange">
      <el-table-column type="selection" fixed="left"/>
      <el-table-column prop="userinfoId" label="编号"/>
      <el-table-column prop="username" label="用户名"/>
      <el-table-column prop="nickname" label="昵称"/>
      <el-table-column prop="phone" label="手机号"/>
      <el-table-column prop="href" label="域名"/>
      <el-table-column prop="profile" label="个性签名" show-overflow-tooltip/>
      <el-table-column prop="status" label="账号状态">
        <template slot-scope="{row}">
          <el-button @click="changeStatus(row.userinfoId)" v-if="row.status === 1" type="success" title="点击切换状态">可用</el-button>
          <el-button @click="changeStatus(row.userinfoId)" v-else type="danger" title="点击切换状态">禁用</el-button>
        </template>
      </el-table-column>
      <el-table-column prop="userinfoStatus" label="实名状态">
        <template slot-scope="{row}">
          <el-tag v-if="row.userinfoStatus === 1">已注册</el-tag>
          <el-tag v-else-if="row.userinfoStatus === 2" type="warning">已提交</el-tag>
          <el-tag v-else-if="row.userinfoStatus === 3" type="success">已实名</el-tag>
          <el-tag v-else type="danger">已拒绝</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="审核">
        <template slot-scope="{row}">
          <el-button @click="exam('exam',row.userinfoId)" v-if="row.userinfoStatus === 2" type="primary">审核</el-button>
          <el-button @click="exam('view',row.userinfoId)" v-else-if="row.userinfoStatus === 3" type="primary">查看</el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作" min-width="160px">
        <template slot-scope="{row}">
          <el-button @click="detail(row.userinfoId)">详情</el-button>
          <el-button @click="del(row.userinfoId)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination :current-page="pageInfo.page" :page-size="pageInfo.limit" :page-sizes="[5,10,20,50,100]" :total="pageInfo.total" layout="total,prev,pager,next,jumper,sizes" @current-change="currentChange" @size-change="sizeChange"/>
  </el-card>
  <vip-detail ref="vipDetail"/>
  <vip-exam ref="vipExam" @close="getTableList"/>
</div>
</template>

<style scoped lang="scss">

</style>
